import { setCookie } from "@/utils";
import * as service from "@/service";
import { ILogin } from "./interface";
import { Dialog } from "vant";

const login: ILogin = {
  actions: {
    async login({ commit }, { values }) {
      const res: any = await service.fetchLogin(values);
      switch (res.status) {
        case 0:
          Dialog.alert({
            message: res?.msg,
          });
          break;
        case 1:
          setCookie("token", res?.token, 30);
          setCookie("userId", res?.userId, 30);
          setCookie("username", values?.username, 30);
          Dialog.confirm({
            message: res.msg + "，是否跳转到首页",
          })
            .then(() => {
              location.href = "/home";
            })
            .catch(() => {
              // on cancel
            });
          break;
        default:
          Dialog.alert({
            message: "发生未知错误",
          });
      }
      commit({
        type: "LOGIN",
        payload: res,
      });
    },
  },
  namespaced: true,
};
export default login;
